package com.dwjf.core.net.callbak;

import android.os.Handler;

import com.dwjf.core.ui.LoaderStyle;

import java.util.IllegalFormatCodePointException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RequestCallBacks implements Callback<String> {
    private final Request request;
    private final Success success;
    private final Error error;
    private final Failure failure;
    private final LoaderStyle loaderStyle;

    private static final Handler HANDLER = new Handler();

    public RequestCallBacks(Request request,Success success,
                            Error error, Failure failure,
                            LoaderStyle loaderStyle){
        this.request = request;
        this.success = success;
        this.error = error;
        this.failure = failure;
        this.loaderStyle = loaderStyle;
    }

    @Override
    public void onResponse(Call<String> call, Response<String> response) {
        if (response.isSuccessful()){
            if (call.isExecuted()){
                if (success != null){
                    success.onSuccess(response.body());
                }
            }
        }else {
            if (error != null){
                error.onError(response.code(), response.message());
            }
        }
        stopLoading();
    }

    @Override
    public void onFailure(Call<String> call, Throwable t) {
        if (failure != null){
            failure.onFailure();
        }
        if (request != null){
            request.onRequestEnd();
        }
        stopLoading();
    }


    private void stopLoading(){
        if (loaderStyle != null){
            HANDLER.postDelayed(new Runnable() {
                @Override
                public void run() {

                }
            },1000);
        }
    }
}
